package com.shmn.gongjiajumain.service.impl;

import com.shmn.gongjiajumain.entity.ProductionLineEntity;
import com.shmn.gongjiajumain.entity.PurchaseIncomeSubmitEntity;
import com.shmn.gongjiajumain.entity.UserEntity;
import com.shmn.gongjiajumain.service.ProductionLineService;
import com.shmn.gongjiajumain.service.UserService;
import com.shmn.gongjiajumain.utils.DateUtil;
import com.shmn.gongjiajumain.vo.purchaseincome.Jig;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseIncomeHistoryTableVo;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseIncomeHistoryUpdateVo;
import com.shmn.gongjiajumain.vo.purchaseincome.PurchaseIncomeTableVo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.Query;

import com.shmn.gongjiajumain.dao.PurchaseIncomeSubmitHistoryDao;
import com.shmn.gongjiajumain.entity.PurchaseIncomeSubmitHistoryEntity;
import com.shmn.gongjiajumain.service.PurchaseIncomeSubmitHistoryService;

import javax.annotation.Resource;


@Service("purchaseIncomeSubmitHistoryService")
public class PurchaseIncomeSubmitHistoryServiceImpl extends ServiceImpl<PurchaseIncomeSubmitHistoryDao, PurchaseIncomeSubmitHistoryEntity> implements PurchaseIncomeSubmitHistoryService {

    @Resource
    private UserService userService;

    @Resource
    private ProductionLineService productionLineService;

    @Override
    public PurchaseIncomeHistoryUpdateVo getByInfoId(Integer id) {
        PurchaseIncomeSubmitHistoryEntity purchaseIncomeSubmitHistoryEntity = baseMapper.selectById(id);
        PurchaseIncomeHistoryUpdateVo purchaseIncomeHistoryUpdateVo = new PurchaseIncomeHistoryUpdateVo();
        BeanUtils.copyProperties(purchaseIncomeSubmitHistoryEntity,purchaseIncomeHistoryUpdateVo);
        purchaseIncomeHistoryUpdateVo.setSubmitName(userService.getById(purchaseIncomeSubmitHistoryEntity.getSubmitId()).getName());
        purchaseIncomeHistoryUpdateVo.setSubmitTime(DateUtil.formatDate(purchaseIncomeSubmitHistoryEntity.getSubmitTime()));
        purchaseIncomeHistoryUpdateVo.setProductionLine(productionLineService.getById(purchaseIncomeSubmitHistoryEntity.getProductionLineId()).getWorkcell());
        purchaseIncomeHistoryUpdateVo.setJigs(getJigs(purchaseIncomeSubmitHistoryEntity.getCode(), purchaseIncomeSubmitHistoryEntity.getCount()));
        purchaseIncomeHistoryUpdateVo.setFirstAcceptorName(userService.getById(purchaseIncomeSubmitHistoryEntity.getFirstAcceptor()).getName());
        purchaseIncomeHistoryUpdateVo.setFirstTime(DateUtil.formatDate(purchaseIncomeSubmitHistoryEntity.getFirstTime()));

        return purchaseIncomeHistoryUpdateVo;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseIncomeSubmitHistoryEntity> page = this.page(
                new Query<PurchaseIncomeSubmitHistoryEntity>().getPage(params),
                new QueryWrapper<PurchaseIncomeSubmitHistoryEntity>()
        );

        PageUtils pageUtils = new PageUtils(page);
        List<PurchaseIncomeHistoryTableVo> purchaseIncomeTableVos = new ArrayList<>();
        Class<PurchaseIncomeSubmitEntity> clazz = PurchaseIncomeSubmitEntity.class;
        pageUtils.getList().forEach(item->{
            PurchaseIncomeHistoryTableVo purchaseIncomeHistoryTableVo = new PurchaseIncomeHistoryTableVo();
            BeanUtils.copyProperties(item,purchaseIncomeHistoryTableVo);
            String submitId = null;
            Integer productionLineId = null;
            String code = null;
            String count = null;
            try {
                Field field1 = clazz.getDeclaredField("submitId");
                Field field2 = clazz.getDeclaredField("productionLineId");
                Field field3 = clazz.getDeclaredField("code");
                Field field4 = clazz.getDeclaredField("count");
                field1.setAccessible(true);
                field2.setAccessible(true);
                field3.setAccessible(true);
                field4.setAccessible(true);
                submitId = (String)field1.get(item);
                productionLineId =(Integer) field2.get(item);
                code = (String) field3.get(item);
                count = (String) field4.get(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String codeAndCount = getCodeAndCount(code, count);
            UserEntity user = userService.getById(submitId);
            ProductionLineEntity productionLine = productionLineService.getById(productionLineId);
            purchaseIncomeHistoryTableVo.setSubmitName(user.getName());
            purchaseIncomeHistoryTableVo.setProductionLine(productionLine.getWorkcell());
            purchaseIncomeHistoryTableVo.setCodeAndCount(codeAndCount);
            purchaseIncomeTableVos.add(purchaseIncomeHistoryTableVo);
        });
        pageUtils.setList(purchaseIncomeTableVos);
        return pageUtils;
    }


    private String getCodeAndCount(String code,String count){
        if (code == null || count == null) return null;
        String[] codes = code.split("\\|");
        String[] counts = count.split("\\|");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < codes.length; i++) {
            builder.append(codes[i] + " - " + counts[i] + "件");
            if (i == codes.length - 1) break;
            builder.append(" | ");
        }
        return builder.toString();
    }

    private List<Jig> getJigs(String code,String count){
        String[] codes = code.split("\\|");
        String[] counts = count.split("\\|");
        List<Jig> jigs = new ArrayList<>();
        for (int i = 0; i < codes.length; i++) {
            Jig jig = new Jig();
            jig.setCode(codes[i]);
            jig.setCount(counts[i]);
            jigs.add(jig);
        }
        return jigs;
    }
}